import {http} from '../../utils/http'
Page({
  data:{
    list:[],
    active: 1,
    show: false,
    num:1,
    count:1,
    // 添加到购物车的数组
    checkedgoods:[],
    propertyChildIds:[]
  },
  async onLoad(e){
    let id = e.id
    console.log(id)
    let cui = await http({url:'shop/goods/detail?id='+id}).catch((err)=>err)
    let list = cui.data.data
    console.log(list)
    this.setData({
      list
    })
  },
  // 添加到购物车
  addcart(){
    // 先从本地获取到
    let cart = wx.getStorageSync('cart')||[]
    // 创建出添加到数组中的对象格式
    let cui = {
      checked:true,
      id:this.data.propertyChildIds.join(','),
      goodsid:this.data.list.basicInfo.id,
      // 图片的路径
      pic:this.data.list.basicInfo.pic,
      // 商品的名称
      name:this.data.list.basicInfo.name,
      // 商品的价格
      minPrice:this.data.list.basicInfo.minPrice,
      count:this.data.count,
      // 获取到选择商品的格式
      props:this.data.checkedgoods.join(' ')
    }
    
    // 购物车去重，根据已有的id  进行判断
    let index = cart.findIndex((item)=>{
      return item.goodsid==cui.goodsid && item.props==cui.props
    })
    if(index==-1){
      cart.unshift(cui)
    }else{
      cart[index].count +=this.data.count
    }
    wx.setStorageSync('cart', cart)
  },
  onChange(e) {
    this.data.count=e.detail
    // console.log(this.data.count)
    // console.log(e.detail)
  },
  goodsCard() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  bind(){
    this.setData({ show: false });
  },
  add(e){
    let index1 = e.currentTarget.dataset.index1
    let index2 = e.currentTarget.dataset.index2
    let item = e.currentTarget.dataset.item
    let item1 = e.currentTarget.dataset.item1
    this.data.list.properties[index1].activeindex = index2
    let name = this.data.list.properties[index1].childsCurGoods[index2].name
    this.data.checkedgoods[index1]= name
    this.data.propertyChildIds[index1] = item.id +':'+item1.id
    this.setData({
      list:this.data.list
    })
  }
})